﻿using System;
using System.Collections.Generic;
using System.Reflection;
using MuYue.ORM.Attributes;


namespace MuYue.ORM
{
    //class OrmBase
    //{
    //    protected OrmBase(string database) {
    //        this.m_Database = database;
    //        this.m_PropetiesChanged = new List<string>();
    //        var ormCache = DbORMCache.SingltonCreate();
    //        DbTable map;
    //        if (ormCache.GetDbTable<OrmBase>(out map)) {
    //            //创建表格检查
    //        }
    //    }


    //    private string m_Database;
    //    private List<string> m_PropetiesChanged;
    //    private DbTable m_MapModle;


    //    public bool Update(params string[] where) {
    //        lock (m_PropetiesChanged) {
    //            //将修改了的数据进行更新操作
    //            m_PropetiesChanged.Clear();
    //            return false;
    //        }
    //    }

    //    public bool Delete(params string[] where) {
    //        return false;
    //    }

    //    public bool Insert() {
    //        return false;
    //    }

    //    protected void OnPropertyChanged(string propertyName) {

    //    }

    //    //构造函数中反射子类中的公开字段和属性
    //    //实现CURD方法
    //    //反射信息依旧采用缓存模式才行
    //}

    /// <summary>
    /// 字段反射后的缓存信息
    /// </summary>
    class DbColumn
    {
        public DbColumn(PropertyInfo info) {
            this.metaInfo = info;
            var attrs = info.GetCustomAttributes(typeof(KeyAttribute), false);
            Key = attrs.Length > 0 ? (KeyAttribute)attrs[0] : null;
            needConvertToInt32 = (info.PropertyType.IsEnum || info.PropertyType == typeof(bool) || info.PropertyType == typeof(bool?));
        }

        /// <summary>
        /// 元数据信息
        /// </summary>
        private readonly PropertyInfo metaInfo;

        /// <summary>
        /// 附加属性的定义
        /// </summary>
        public KeyAttribute Key { set; get; }

        /// <summary>
        /// 是否是需要转换为整数型来存储的数据
        /// </summary>
        private bool needConvertToInt32;

        /// <summary>
        /// 获取数据的类型
        /// </summary>
        /// <returns></returns>
        public Type ThePropertyType { get { return metaInfo.PropertyType; } }

        /// <summary>
        /// 设置值
        /// </summary>
        /// <param name="obj">实例对象</param>
        /// <param name="value">属性值</param>
        public void SetValue(object obj, object value) {
            if (value is DBNull)
                return;
            if (needConvertToInt32 && !ThePropertyType.IsEnum)
                value = Convert.ToBoolean(value);
            metaInfo.SetValue(obj, value, null);
        }

        /// <summary>
        /// 取出实例该属性的值
        /// </summary>
        /// <param name="obj">实例对象</param>
        /// <returns></returns>
        public object GetValue(object obj) {
            var value = metaInfo.GetValue(obj, null);
            return needConvertToInt32 ? Convert.ToInt32(value) : value;
        }
    }
}
